package com.cloudsigns.factorymanager2.control.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.LoginActivity;
import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.control.fragment.AnalyFragment;
import com.cloudsigns.factorymanager2.control.fragment.ControlFragment;
import com.cloudsigns.factorymanager2.control.fragment.PanelMapFragment;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.produce.activity.AvatarImageShowerActivity;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.customview.GlideCircleTransform;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.fragment.InternalOfficeFragment;
import com.cloudsigns.factorymanager2.project.fragment.ProjectManagerFragment;
import com.cloudsigns.factorymanager2.project.fragment.ProjectOrderListFragment;
import com.cloudsigns.factorymanager2.project.fragment.SettingFragmet;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

public class ControlFragmentActivity extends FragmentActivity implements View.OnClickListener{
    private View ll_op_one, ll_op_two, ll_op_three, ll_op_four;
    private ImageView img_op_one, img_op_two, img_op_three, img_op_four;
    private ImageView img_tri_one, img_tri_two, img_tri_three, img_tri_four;
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View rl_head;
    private ImageView img_head;
    private TextView tv_headtop, tv_headbottom;
    private CustomViewPager viewpager;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    private ImageView img_project_manager, main_project_order, main_project_office, main_project_systems;
    private RelativeLayout layout_project_manager, layout_project_order, layout_project_office, layout_project_systems;
    private TextView textview_project_manager,tv_project_order,textview_call,tv_project_systems;
    private Fragment  controlFragment, analyFragment, systemFragment;
    private PanelMapFragment panelMapFragment;
    private PageIndicatorView indicator;
    private DropDownPoupWindow popupWindow;

    public static final int PROJECT_MANAGENT = 0;
    public static final int WORK_SHEETS_LIST = 1;
    public static final int INNER_OFFICE = 2;
    public static final int SYSTEM = 3;
    public int currentIndex = PROJECT_MANAGENT;
    private String TAG = "ProjectMainActivity";
    private AccountInfoBean accountInfoBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_control);

        init();

        accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        if (null != accountInfoBean) {
            if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {
                tv_headbottom.setText("项目管理/" + accountInfoBean.getAccountName());

                String companyName = accountInfoBean.getCompanyName();
                if (!TextUtils.isEmpty(companyName) && !"null".equals(companyName)) {
                    tv_headtop.setText(companyName);
                }
                Glide.with(this).load(accountInfoBean.getAvatar()).bitmapTransform(new GlideCircleTransform(this)).crossFade(1000).error(R.mipmap.default_avatar).into(img_head);
            }
        }
        viewpager.setCurrentItem(PROJECT_MANAGENT);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            boolean isExit = intent.getBooleanExtra(ConstantUtil.KEY_1, false);
            if (isExit) {
                Intent mItent = new Intent(this, LoginActivity.class);
                startActivity(mItent);
                this.finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    private void init() {
        ll_op_one = findViewById(R.id.ll_op_one);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_three = findViewById(R.id.ll_op_three);
        ll_op_four = findViewById(R.id.ll_op_four);
        img_op_one = findViewById(R.id.img_op_one);
        img_op_two = findViewById(R.id.img_op_two);
        img_op_three = findViewById(R.id.img_op_three);
        img_op_four = findViewById(R.id.img_op_four);
        img_tri_one = findViewById(R.id.img_tri_one);
        img_tri_two = findViewById(R.id.img_tri_two);
        img_tri_three = findViewById(R.id.img_tri_three);
        img_tri_four = findViewById(R.id.img_tri_four);
        rl_back = findViewById(R.id.rl_back);
        rl_head = findViewById(R.id.rl_head);
        tv_headtop = findViewById(R.id.tv_headtop);
        tv_headbottom = findViewById(R.id.tv_headbottom);
        img_head = findViewById(R.id.img_head);
        tv_title = findViewById(R.id.tv_title);
        img_project_manager = findViewById(R.id.img_project_manager);
        main_project_order = findViewById(R.id.main_project_order);
        main_project_office = findViewById(R.id.main_project_office);
        main_project_systems = findViewById(R.id.main_project_systems);
        layout_project_manager = findViewById(R.id.layout_project_manager);
        layout_project_order = findViewById(R.id.layout_project_order);
        layout_project_office = findViewById(R.id.layout_project_office);
        layout_project_systems = findViewById(R.id.layout_project_systems);
        textview_project_manager=findViewById(R.id.textview_project_manager);
        tv_project_order=findViewById(R.id.tv_project_order);
        textview_call=findViewById(R.id.textview_call);
        tv_project_systems=findViewById(R.id.tv_project_systems);
        viewpager = findViewById(R.id.viewpager);
        viewpager.setScanScroll(false);
        indicator = findViewById(R.id.indicator);


        tv_title.setVisibility(View.GONE);
        rl_head.setVisibility(View.VISIBLE);
        rl_back.setVisibility(View.GONE);

        img_head.setOnClickListener(this);
        layout_project_manager.setOnClickListener(this);
        layout_project_order.setOnClickListener(this);
        layout_project_office.setOnClickListener(this);
        layout_project_systems.setOnClickListener(this);
        ll_op_one.setOnClickListener(this);
        ll_op_two.setOnClickListener(this);
        ll_op_three.setOnClickListener(this);
        ll_op_four.setOnClickListener(this);

        panelMapFragment = new PanelMapFragment();
        controlFragment = new ControlFragment();
        analyFragment = new AnalyFragment();
        systemFragment = new SettingFragmet();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(panelMapFragment);
        listFragments.add(controlFragment);
        listFragments.add(analyFragment);
        listFragments.add(systemFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(4);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_17a5ef));
        viewpager.setCurrentItem(PROJECT_MANAGENT);
        showSelectedOption(PROJECT_MANAGENT);

        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
                currentIndex = position;

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        img_op_one.setBackgroundResource(R.mipmap.title_icon_menu);
        img_op_two.setBackgroundResource(R.mipmap.icon_hand);
        img_op_three.setBackgroundResource(R.mipmap.icon_produced);
        img_op_four.setBackgroundResource(R.mipmap.icon_patrol);

        ll_op_one.setVisibility(View.VISIBLE);
        ll_op_two.setVisibility(View.GONE);
        ll_op_three.setVisibility(View.GONE);
        ll_op_four.setVisibility(View.GONE);

    }

    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            // 项目管理
            case PROJECT_MANAGENT:
                layout_project_manager.setSelected(true);
                img_project_manager.setImageDrawable(getResources().getDrawable(R.mipmap.tab_engineering_pressed));
                textview_project_manager.setTextColor(getResources().getColor(R.color.rgb_17a5ef));

                layout_project_order.setSelected(false);
                main_project_order.setImageDrawable(getResources().getDrawable(R.mipmap.tab_manage_normal));
                tv_project_order.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_office.setSelected(false);
                main_project_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_office_normal));
                textview_call.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_systems.setSelected(false);
                main_project_systems.setImageDrawable(getResources().getDrawable(R.mipmap.tab_setting_normal));
                tv_project_systems.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                ll_op_one.setVisibility(View.VISIBLE);
                ll_op_two.setVisibility(View.GONE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.GONE);
                break;

            // 工单列表
            case WORK_SHEETS_LIST:
                layout_project_manager.setSelected(false);
                img_project_manager.setImageDrawable(getResources().getDrawable(R.mipmap.tab_engineering_normal));
                textview_project_manager.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_order.setSelected(true);
                main_project_order.setImageDrawable(getResources().getDrawable(R.mipmap.tab_manage_pressed));
                tv_project_order.setTextColor(getResources().getColor(R.color.rgb_17a5ef));

                layout_project_office.setSelected(false);
                main_project_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_office_normal));
                textview_call.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_systems.setSelected(false);
                main_project_systems.setImageDrawable(getResources().getDrawable(R.mipmap.tab_setting_normal));
                tv_project_systems.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.GONE);
                break;

            // 内部办公
            case INNER_OFFICE:
                layout_project_manager.setSelected(false);
                img_project_manager.setImageDrawable(getResources().getDrawable(R.mipmap.tab_engineering_normal));
                textview_project_manager.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_order.setSelected(false);
                main_project_order.setImageDrawable(getResources().getDrawable(R.mipmap.tab_manage_normal));
                tv_project_order.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_office.setSelected(true);
                main_project_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_office_pressed));
                textview_call.setTextColor(getResources().getColor(R.color.rgb_17a5ef));

                layout_project_systems.setSelected(false);
                main_project_systems.setImageDrawable(getResources().getDrawable(R.mipmap.tab_setting_normal));
                tv_project_systems.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.GONE);
                break;

            // 系统
            case SYSTEM:
                layout_project_manager.setSelected(false);
                img_project_manager.setImageDrawable(getResources().getDrawable(R.mipmap.tab_engineering_normal));
                textview_project_manager.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_order.setSelected(false);
                main_project_order.setImageDrawable(getResources().getDrawable(R.mipmap.tab_manage_normal));
                tv_project_order.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_office.setSelected(false);
                main_project_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_office_normal));
                textview_call.setTextColor(getResources().getColor(R.color.color_a2a2a4));

                layout_project_systems.setSelected(true);
                main_project_systems.setImageDrawable(getResources().getDrawable(R.mipmap.tab_setting_pressed));
                tv_project_systems.setTextColor(getResources().getColor(R.color.rgb_17a5ef));

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.GONE);
                break;

        }

    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 头像
            case R.id.img_head:
                Intent intent = new Intent(ControlFragmentActivity.this, AvatarImageShowerActivity.class);
                intent.putExtra("imageUrl", accountInfoBean.getAvatar());
                startActivity(intent);
                break;
            case R.id.ll_op_one:
                //showModulePopupWindow(view);
                break;

            case R.id.layout_project_manager:
                if (currentIndex != PROJECT_MANAGENT) {

                    currentIndex = PROJECT_MANAGENT;
                    viewpager.setCurrentItem(PROJECT_MANAGENT);
                    showSelectedOption(PROJECT_MANAGENT);
                }
                break;

            case R.id.layout_project_order:
                if (currentIndex != WORK_SHEETS_LIST) {
                    currentIndex = WORK_SHEETS_LIST;

                    viewpager.setCurrentItem(WORK_SHEETS_LIST);
                    showSelectedOption(WORK_SHEETS_LIST);
                }
                break;

            case R.id.layout_project_office:
                if (currentIndex != INNER_OFFICE) {
                    currentIndex = INNER_OFFICE;

                    viewpager.setCurrentItem(INNER_OFFICE);
                    showSelectedOption(INNER_OFFICE);
                }
                break;

            case R.id.layout_project_systems:
                if (currentIndex != SYSTEM) {
                    currentIndex = SYSTEM;

                    viewpager.setCurrentItem(SYSTEM);
                    showSelectedOption(SYSTEM);
                }
                break;
        }
    }
}
